09. Spring Boot Form Validation

035ND C01 L03 A27 VALIDATION

Instructions

  1. Goto https://start.spring.io/ and create a Spring Boot project named: spring-boot-validation. With web and thymeleaf, devtool as dependency.
  2. Create a User model. Please generate the getter and setter (important)
import javax.validation.constraints.NotBlank;

public class User {
   @NotBlank(message = "username cannot be empty")
   private String name;
   @NotBlank(message = "password cannot be empty")
   private String password;
   private Double grade;

   public User(String name, String password, Double grade) {
       this.name = name;
       this.password = password;
       this.grade = grade;
   }
}
  • Create a UserController
package com.example.springbootvalidation.controller;

import com.example.springbootvalidation.model.User;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.validation.Valid;

@Controller
public class UserController {

   @RequestMapping("add")
   public String toAdd(User user) {
       return "add";
   }

   @RequestMapping("addUser")
   public String add(@Valid User user, BindingResult result) {
       if (result.hasErrors()) {
           return "add";
       }
       System.out.println("Save user =" + user);
       return "success";
   }
}
  • Add add.html in templates
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.w3.org/1999/xhtml">
<head>
   <meta charset="UTF-8">
   <title>Add User</title>
</head>
<body>
<h3>Add User</h3>

<form action="addUser" method="post">
Username: <input type="text" name="name" />
<span th:errors="${user.name}" style="color:red"></span><br />
Password: <input type="password" name="password"/><br />
<span th:errors="${user.password}" style="color:red"></span><br />
Grade: <input type="text" name="grade" /><br />
<input type="submit" value="Add" >
</form>
</body>
</html>
  • Add success.html in templates
<!DOCTYPE html>
<html lang="en">
<head>
   <meta charset="UTF-8">
   <title>Successful</title>
</head>
<body>
User successfully added.
</body>
</html>

035ND C01 L03 A28 VALIDATION EXAMPLE EXPLAIN

Spring Boot Validation Quiz

Object is used by a request processing method to check validation failure

SOLUTION: BindingResult

035ND C01 L03 A29 VALIDATION DEMO

Spring Boot Validation Common Annotations

035ND C01 L03 A30 VALIDATION ANNOTATIONS V2

Common validation annotations:

@NotBlank: check if string is null or empty after trimmed the front and end spaces.

@NotEmpty: check if string is null or empty without trim the front and end spaces.

@Length: check string length, include max and min.

@Min: check min, cannot be less. for instance if @Min(0), then input should not less than 0.

@Max: check max, cannot be over.

@Emai: check email format, should be XX@XX.XX

Spring Boot Validation Common Annotations Example Prep

Update User to use code below, again please generate the getter and setter yourself (important)

import org.hibernate.validator.constraints.Length;

import javax.validation.constraints.Email;
import javax.validation.constraints.Max;
import javax.validation.constraints.Min;
import javax.validation.constraints.NotBlank;

public class User {
   @NotBlank(message = "username cannot be empty")
   private String name;
   @NotBlank(message = "password cannot be empty")
   @Length(min=6, max=10, message="length of password should between 6 to 10 characters")
   private String password;
   @Min(value=0)
   @Max(value=100)
   private Double grade;
   @Email
   private String email;

   public User(String name, String password, Double grade, String email) {
       this.name = name;
       this.password = password;
       this.grade = grade;
       this.email = email;
   }
}

Update add.html to code below

<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.w3.org/1999/xhtml">
<head>
   <meta charset="UTF-8">
   <title>Add User</title>
</head>
<body>
<h3>Add User</h3>

<form action="addUser" method="post">
<span th:errors="${user.name}" style="color:red"></span><br />
Username: <input type="text" name="name" /><br />
<span th:errors="${user.password}" style="color:red"></span><br />
Password: <input type="password" name="password"/><br />
<span th:errors="${user.grade}" style="color:red"></span><br />
Grade: <input type="text" name="grade" /><br />
<span th:errors="${user.email}" style="color:red"></span><br />
Email: <input type="text" name="email" /><br />
<input type="submit" value="Add" >
</form>
</body>
</html>

035ND C01 L03 A31 VALIDATION ANNOTATIONS EXAMPLE

Spring Boot Validation Common Annotations Quiz

If we create a field, this field should allow string like “ “, but does not allow null string. Which validation annotation I should use.

SOLUTION: @NotEmpty

035ND C01 L03 A32 LESSON RECAP V2